#include "reader.h"
#include <QDebug>

Reader::Reader(int id, Library *library, int readingTime, int sleepingTime) :
    QRunnable(), m_id(id), m_library(library),
    m_readingTimeMSecs(readingTime),
    m_sleepingTime(sleepingTime)
{
}

void Reader::run()
{
    while (true)
    {
        int wantBook = qrand() % m_library->size();
        if(!m_library->takeBookForReading(wantBook)) {
            continue;
        }
        readBook(wantBook);
    }
}

void Reader::readBook(book_id_t book)
{
    qDebug() << "Reader " << m_id << " is reading book #" << book;
    msleep(m_readingTimeMSecs);
    m_library->returnReadBook(book);
    qDebug() << "Reader " << m_id << " finished reading book #" << book;
}
